<template>
	<div>
		<el-dialog v-model="showAddPublicDialog" title="添加设备" width="400">
			<el-form :label-width="100" label-position="top" style="width: 300px">
				<el-form-item label="设备名称">
					<el-input v-model.trim="publicDetials.publicName" />
				</el-form-item>
				<el-form-item label="楼栋">
					<el-input :value="publicDetials.buildId" readonly/>
				</el-form-item>
				<el-form-item label="设备数量">
					<el-input-number v-model.number="publicDetials.publicCount" :min="1" :max="10" />
				</el-form-item>
			</el-form>
			<template #footer>
				<el-space :size="30">
					<el-button type="primary" plain @click="handleAddPublic">添加</el-button>
					<el-button type="danger" plain @click="showAddPublicDialog = false">取消</el-button>
				</el-space>
			</template>
		</el-dialog>
	</div>
</template>

<script setup>
import { addPublicEquiment,getAllPublicEquiment } from '@/utils/axios/domiRequests'
import { getPubOneBuild } from '@/utils/axios/getTotal';
import { ElMessage } from 'element-plus'
import { reactive } from 'vue'
import { useUserStore } from '@/stores/userStore';
import { debounce } from '@/utils/tools';

const {userInfo}=useUserStore()
const $emit=defineEmits(['add-res'])

// 添加公共设施的信息
const publicDetials = reactive({
	buildId: userInfo.buildId,
	publicName: '',
	publicCount: 1,
})

const showAddPublicDialog = defineModel('showAddPublicDialog')

// 处理添加设备
const handleAddPublic = debounce(async () => {
	if (!publicDetials.publicName) return ElMessage.warning({ message: '设备名不能为空', duration: 1000 })
	publicDetials.buildId=Number.parseInt(publicDetials.buildId)
	await addPublicEquiment(publicDetials)
	let { data } = await getAllPublicEquiment({ currentPage: 1 })
	let res = await getPubOneBuild({ buildId: userInfo?.buildId })
	showAddPublicDialog.value = false
	$emit("add-res",{data,tota:res.data})
},600)
</script>

<style scoped lang="scss">
.el {
	&-form {
		width: 300px;
		margin: auto;
	}
	&-input {
		width: 150px;
	}
}
</style>
